#include<iostream>
using namespace std;

struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
 };


//使用快慢指针
//1 2 3 4 5 6 7，需要删除5
 ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode* fastNode = head;
    ListNode* slowNode = head;
    ListNode* pre = head;
    for(int i=0;i<n;i++)
    {
        fastNode = fastNode->next;
    }
    while(fastNode -> next)
    {
        fastNode = fastNode->next;
        slowNode = slowNode->next;
    }
    slowNode->next = slowNode->next->next;
    return pre;
}


int main(){
    ListNode* head = new ListNode(1);
    ListNode* cur = head;
    cout<<"enter ListNode:"<<endl;
    for(int i=0;i<1;++i){
        int val;
        cin>>val;
        cur->next = new ListNode(val);
        cur=cur->next;
    }
    cur = head;
    cout<<"ListNode is:"<<endl;
    for(int i=0;i<2;i++)
    {
        cout<<cur -> val<<" ";
        cur = cur->next;
    }
    ListNode* result = removeNthFromEnd(head, 1);
    cout<<"\nresult is:"<<endl;
    while(result)
    {
        cout<<result->val<<" ";
        result = result->next;
    }
    return 0;
}